package com.skt.common_hr.biztrip_sac.model
{
	import com.skt.common_hr.biztrip_sac.BizTripFacade;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;

	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class GasCostProxy extends CommonProxy
	{
		public static const NAME:String = "GasCostProxy";

		public var pernr:String;
		public var gasCost:Number;
		public var wares:String = "KRW";
		public var errCode:int;
		public var errText:String;


		//이름
		public var name:String;

		//부서명(상위조직명 포함)
		public var stext:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function GasCostProxy(data:Object=null)
		{
			super(NAME, data);
		}


		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 *
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, date:Date):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = this.pernr = pernr;
			param["I_DATE"]  = DateUtil.toBasicString(date);

			loadData("Z_HR_T008", param, result, fault);
		}



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			if(event.result["RETCODE"]==0) {
				if(event.result.OTAB != "") {
					gasCost = parseGasCost(event.result.OTAB[0]);
				}
				facade.sendNotification(BizTripFacade.LOAD_GASCOST_COMPLETE, this);
			}
			else {
				trace( "failed to 유류대 단가");
				gasCost = 0;
				facade.sendNotification(BizTripFacade.LOAD_GASCOST_FAILED, this);
			}
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			errCode = -1;
			errText = "호출실패";
			gasCost = 0;
			facade.sendNotification(BizTripFacade.LOAD_GASCOST_FAILED, this);
		}





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 *
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseGasCost(retObj:Object):Number
		{
			var amount:Number = retObj["AMT"];
			amount = Math.round(amount*100);
			return amount;
		}

	}
}